#!/bin/bash

BASE_PATH=`pwd`
DEVOPS_CONFIG=$BASE_PATH/devops.ini

DEFAULT_LOCATION=`awk -F '=' '/\[Common\]/{a=1}a==1&&$1~/DEFAULT_LOCATION/{print $2;exit}' $DEVOPS_CONFIG`
PASSWORD=`awk -F '=' '/\[MySQL\]/{a=1}a==1&&$1~/PASSWORD/{print $2;exit}' $DEVOPS_CONFIG`

VOLUMN_LOCATION=$DEFAULT_LOCATION/mysql

mkdir -p $VOLUMN_LOCATION/{data,conf}

CONF_LOCATION=$VOLUMN_LOCATION/conf
DATA_LOCATION=$VOLUMN_LOCATION/data

rm -rf $CONF_LOCATION/my.cnf
cp ./my.cnf $CONF_LOCATION/my.cnf

docker run -itd --name mysql -v $DATA_LOCATION:/var/lib/mysql -v /etc/localtime:/etc/localtime -v $CONF_LOCATION/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=$PASSWORD -p 3306:3306 --restart always mysql:5.7 --default-authentication-plugin=mysql_native_password --lower_case_table_names=1

# 执行sql需要等待服务器启动
sleep 10s

docker cp init-mysql.sql mysql:/init-mysql.sql
docker exec mysql mysql  -uroot -p$PASSWORD -e "source /init-mysql.sql"
